package com.joker.repository;

import com.mysql.cj.xdevapi.JsonArray;
import net.sf.json.JSONObject;

import java.sql.*;
import java.util.ArrayList;

public class RootSearchZg {
    String name=null;
    Connection connection= null;
    JsonArray array=new JsonArray();

    ResultSet resultSet=null;
    ArrayList<JSONObject> list=new ArrayList<>();
    public ArrayList<JSONObject> show(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/huizhan?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
            String root="root";
            String password="123456";
            try {
                connection = DriverManager.getConnection(url,root,password);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            String sql="select * from user_zhanguan ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);


            resultSet=preparedStatement.executeQuery();
            ResultSetMetaData metaData=resultSet.getMetaData();
            int sum =metaData.getColumnCount();
            while(resultSet.next()){
                JSONObject obj=new JSONObject();
                String username=resultSet.getString(2);
                String zgname=resultSet.getString(3);
                int zhuangtai=resultSet.getInt(4);
                obj.put("username",username);
                obj.put("zgname",zgname);
                obj.put("zhuangtai",zhuangtai);
                list.add(obj);


            }
            System.out.println("list列表的值"+list);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return list;
    }
}
